﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using whatpricemyhouse.LINQ;
namespace whatpricemyhouse.agent
{
    public partial class ManageAgent : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            new AdminBasicPage();
            if (Page.User.Identity.IsAuthenticated)
            {
                if (!IsPostBack) {
                    LoadAgent();
                    ImageButton UpdateButton1 = this.LoginView2.FindControl("UpdateButton1") as ImageButton;
                    ImageButton UpdateButton2 = this.LoginView2.FindControl("UpdateButton2") as ImageButton;
                    ImageButton UpdateButton3 = this.LoginView2.FindControl("UpdateButton3") as ImageButton;
                    ImageButton sendEmailButton1 = this.LoginView2.FindControl("sendEmailButton1") as ImageButton;
                    ImageButton sendEmailButton2 = this.LoginView2.FindControl("sendEmailButton2") as ImageButton;
                    ImageButton sendEmailButton3 = this.LoginView2.FindControl("sendEmailButton3") as ImageButton;

                    UpdateButton1.Attributes.Add("onmouseover", "mouseOver1(this);");
                    UpdateButton1.Attributes.Add("onmouseout", "mouseOut1(this);");
                    UpdateButton2.Attributes.Add("onmouseover", "mouseOver1(this);");
                    UpdateButton2.Attributes.Add("onmouseout", "mouseOut1(this);");
                    UpdateButton3.Attributes.Add("onmouseover", "mouseOver1(this);");
                    UpdateButton3.Attributes.Add("onmouseout", "mouseOut1(this);");
                }
            }
        }

        protected void UpdateButton1_Click(object sender, ImageClickEventArgs e)
        {
            DataControllerDataContext dcdc = new DataControllerDataContext();
            HiddenField agentId1 = this.LoginView2.FindControl("agentId1") as HiddenField;
            FileUpload photoUpload1 = this.LoginView2.FindControl("photoUpload1") as FileUpload;
            
            String imageUrl = uploadFile(photoUpload1);
            TextBox addressTxt1 = this.LoginView2.FindControl("addressTxt1") as TextBox;
            TextBox nameTxt1 = this.LoginView2.FindControl("nameTxt1") as TextBox;
            TextBox mobileTxt1 = this.LoginView2.FindControl("mobileTxt1") as TextBox;
            TextBox phoneTxt1 = this.LoginView2.FindControl("phoneTxt1") as TextBox;
            TextBox faxTxt1 = this.LoginView2.FindControl("faxTxt1") as TextBox;
            TextBox textTxt1 = this.LoginView2.FindControl("textTxt1") as TextBox;
            TextBox linkTxt1 = this.LoginView2.FindControl("linkTxt1") as TextBox;
            CheckBox isShownEmailCheck1 = this.LoginView2.FindControl("isShownEmailCheck1") as CheckBox;

            var query = from a in dcdc.agent_info_tables
                        where a.UserID == new Guid(agentId1.Value)
                        select a;

            foreach (agent_info_table ait in query) {
                if (!String.IsNullOrEmpty(imageUrl))
                {
                    ait.Photo = imageUrl;
                }
                ait.address = addressTxt1.Text;
                ait.FirstName = nameTxt1.Text;
                ait.Phone = phoneTxt1.Text;
                ait.Mobile = mobileTxt1.Text;
                ait.Fax = faxTxt1.Text;
                ait.Text = textTxt1.Text;
                ait.link = linkTxt1.Text;
                ait.IsShowEmail = isShownEmailCheck1.Checked;
                break;
            }

                    
            dcdc.SubmitChanges();
            LoadAgent();




        }

        protected void UpdateButton2_Click(object sender, ImageClickEventArgs e)
        {
            DataControllerDataContext dcdc = new DataControllerDataContext();
            HiddenField agentId2 = this.LoginView2.FindControl("agentId2") as HiddenField;
            if (!String.IsNullOrEmpty(agentId2.Value))
            {
                FileUpload photoUpload2 = this.LoginView2.FindControl("photoUpload2") as FileUpload;
                String imageUrl = uploadFile(photoUpload2);
                TextBox addressTxt2 = this.LoginView2.FindControl("addressTxt2") as TextBox;
                TextBox nameTxt2 = this.LoginView2.FindControl("nameTxt2") as TextBox;
                TextBox mobileTxt2 = this.LoginView2.FindControl("mobileTxt2") as TextBox;
                TextBox phoneTxt2 = this.LoginView2.FindControl("phoneTxt2") as TextBox;
                TextBox faxTxt2 = this.LoginView2.FindControl("faxTxt2") as TextBox;
                TextBox textTxt2 = this.LoginView2.FindControl("textTxt2") as TextBox;
                TextBox linkTxt2 = this.LoginView2.FindControl("linkTxt2") as TextBox;
                CheckBox isShownEmailCheck2 = this.LoginView2.FindControl("isShownEmailCheck2") as CheckBox;


                var query = from a in dcdc.agent_info_tables
                            where a.UserID == new Guid(agentId2.Value)
                            select a;

                foreach (agent_info_table ait in query)
                {
                    if (!String.IsNullOrEmpty(imageUrl))
                    {
                        ait.Photo = imageUrl;
                    }
                    ait.address = addressTxt2.Text;
                    ait.FirstName = nameTxt2.Text;
                    ait.Phone = phoneTxt2.Text;
                    ait.Mobile = mobileTxt2.Text;
                    ait.Fax = faxTxt2.Text;
                    ait.Text = textTxt2.Text;
                    ait.link = linkTxt2.Text;
                    ait.IsShowEmail = isShownEmailCheck2.Checked;
                    break;
                }


                dcdc.SubmitChanges();
                LoadAgent();
            }
        }

        protected void UpdateButton3_Click(object sender, ImageClickEventArgs e)
        {
            DataControllerDataContext dcdc = new DataControllerDataContext();
            HiddenField agentId3 = this.LoginView2.FindControl("agentId3") as HiddenField;
            if (!String.IsNullOrEmpty(agentId3.Value))
            {
                FileUpload photoUpload3 = this.LoginView2.FindControl("photoUpload3") as FileUpload;
                String imageUrl = uploadFile(photoUpload3);
                TextBox addressTxt3 = this.LoginView2.FindControl("addressTxt3") as TextBox;
                TextBox nameTxt3 = this.LoginView2.FindControl("nameTxt3") as TextBox;
                TextBox mobileTxt3 = this.LoginView2.FindControl("mobileTxt3") as TextBox;
                TextBox phoneTxt3 = this.LoginView2.FindControl("phoneTxt3") as TextBox;
                TextBox faxTxt3 = this.LoginView2.FindControl("faxTxt3") as TextBox;
                TextBox textTxt3 = this.LoginView2.FindControl("textTxt3") as TextBox;
                TextBox linkTxt3 = this.LoginView2.FindControl("linkTxt3") as TextBox;
                CheckBox isShownEmailCheck3 = this.LoginView2.FindControl("isShownEmailCheck3") as CheckBox;

                var query = from a in dcdc.agent_info_tables
                            where a.UserID == new Guid(agentId3.Value)
                            select a;

                foreach (agent_info_table ait in query)
                {
                    if (!String.IsNullOrEmpty(imageUrl))
                    {
                        ait.Photo = imageUrl;
                    }
                    ait.address = addressTxt3.Text;
                    ait.FirstName = nameTxt3.Text;
                    ait.Phone = phoneTxt3.Text;
                    ait.Mobile = mobileTxt3.Text;
                    ait.Fax = faxTxt3.Text;
                    ait.Text = textTxt3.Text;
                    ait.link = linkTxt3.Text;
                    ait.IsShowEmail = isShownEmailCheck3.Checked;
                    break;
                }


                dcdc.SubmitChanges();
                LoadAgent();
            }
        }

        private void LoadAgent()
        {
            DataControllerDataContext dcdc = new DataControllerDataContext();
            //get current user id
            MembershipUser myObject = Membership.GetUser();
            string UserID = myObject.ProviderUserKey.ToString();
            Guid currtneUser = new Guid(UserID);

            //get user place id

            var agents = from a in dcdc.place_agent_tables
                         where a.UserID == currtneUser
                         select a;

            int placeID = 42;
            foreach (place_agent_table pat in agents)
            {
                placeID = pat.suburbid.Value;
            }

            //get user agent info
            //----first agent
            var agent1 = from a in dcdc.agent_info_tables
                         where a.UserID == currtneUser
                         select a;

            Image Image1 = this.LoginView2.FindControl("Image1") as Image;
            Label nameLabel1 = this.LoginView2.FindControl("nameLabel1") as Label;
            Label phoneLabel1 = this.LoginView2.FindControl("phoneLabel1") as Label;
            Label mobileLabel1 = this.LoginView2.FindControl("mobileLabel1") as Label;
            Label facsimileLabel1 = this.LoginView2.FindControl("facsimileLabel1") as Label;
            HyperLink sendEmailButton1 = this.LoginView2.FindControl("sendEmailButton1") as HyperLink;


            TextBox addressTxt1 = this.LoginView2.FindControl("addressTxt1") as TextBox;
            TextBox nameTxt1 = this.LoginView2.FindControl("nameTxt1") as TextBox;
            TextBox mobileTxt1 = this.LoginView2.FindControl("mobileTxt1") as TextBox;
            TextBox phoneTxt1 = this.LoginView2.FindControl("phoneTxt1") as TextBox;
            TextBox faxTxt1 = this.LoginView2.FindControl("faxTxt1") as TextBox;
            TextBox textTxt1 = this.LoginView2.FindControl("textTxt1") as TextBox;
            TextBox linkTxt1 = this.LoginView2.FindControl("linkTxt1") as TextBox;
            CheckBox isShownEmailCheck1 = this.LoginView2.FindControl("isShownEmailCheck1") as CheckBox;
            HiddenField agentId1 = this.LoginView2.FindControl("agentId1") as HiddenField;


            foreach (agent_info_table ait in agent1)
            {
                Image1.ImageUrl = ait.Photo;
                phoneLabel1.Text = ait.Phone;
                mobileLabel1.Text = ait.Mobile;
                facsimileLabel1.Text = ait.Fax;
                nameLabel1.Text = ait.address;
                agentId1.Value = ait.UserID.ToString();

                addressTxt1.Text = ait.address;
                nameTxt1.Text = ait.FirstName;
                mobileTxt1.Text = ait.Mobile;
                phoneTxt1.Text = ait.Phone;
                faxTxt1.Text = ait.Fax;
                textTxt1.Text = ait.Text;
                linkTxt1.Text = ait.link;
                isShownEmailCheck1.Checked = ait.IsShowEmail.Value;

                break;
            }

            //get user email.
            MembershipUser u1 = Membership.GetUser(User.Identity.Name);
            sendEmailButton1.NavigateUrl = "mailto:" + u1.Email;



            //get second agent
            Image Image2 = this.LoginView2.FindControl("Image2") as Image;
            Label nameLabel2 = this.LoginView2.FindControl("nameLabel2") as Label;
            Label phoneLabel2 = this.LoginView2.FindControl("phoneLabel2") as Label;
            Label mobileLabel2 = this.LoginView2.FindControl("mobileLabel2") as Label;
            Label facsimileLabel2 = this.LoginView2.FindControl("facsimileLabel2") as Label;
            HyperLink sendEmailButton2 = this.LoginView2.FindControl("sendEmailButton2") as HyperLink;
            HiddenField agentId2 = this.LoginView2.FindControl("agentId2") as HiddenField;
            TextBox addressTxt2 = this.LoginView2.FindControl("addressTxt2") as TextBox;
            TextBox nameTxt2 = this.LoginView2.FindControl("nameTxt2") as TextBox;
            TextBox mobileTxt2 = this.LoginView2.FindControl("mobileTxt2") as TextBox;
            TextBox phoneTxt2 = this.LoginView2.FindControl("phoneTxt2") as TextBox;
            TextBox faxTxt2 = this.LoginView2.FindControl("faxTxt2") as TextBox;
            TextBox textTxt2 = this.LoginView2.FindControl("textTxt2") as TextBox;
            TextBox linkTxt2 = this.LoginView2.FindControl("linkTxt2") as TextBox;
            CheckBox isShownEmailCheck2 = this.LoginView2.FindControl("isShownEmailCheck2") as CheckBox;

            Image Image3 = this.LoginView2.FindControl("Image3") as Image;
            Label nameLabel3 = this.LoginView2.FindControl("nameLabel3") as Label;
            Label phoneLabel3 = this.LoginView2.FindControl("phoneLabel3") as Label;
            Label mobileLabel3 = this.LoginView2.FindControl("mobileLabel3") as Label;
            Label facsimileLabel3 = this.LoginView2.FindControl("facsimileLabel3") as Label;
            HyperLink sendEmailButton3 = this.LoginView2.FindControl("sendEmailButton3") as HyperLink;
            HiddenField agentId3 = this.LoginView2.FindControl("agentId3") as HiddenField;
            TextBox addressTxt3 = this.LoginView2.FindControl("addressTxt3") as TextBox;
            TextBox nameTxt3 = this.LoginView2.FindControl("nameTxt3") as TextBox;
            TextBox mobileTxt3 = this.LoginView2.FindControl("mobileTxt3") as TextBox;
            TextBox phoneTxt3 = this.LoginView2.FindControl("phoneTxt3") as TextBox;
            TextBox faxTxt3 = this.LoginView2.FindControl("faxTxt3") as TextBox;
            TextBox textTxt3 = this.LoginView2.FindControl("textTxt3") as TextBox;
            TextBox linkTxt3 = this.LoginView2.FindControl("linkTxt3") as TextBox;
            CheckBox isShownEmailCheck3 = this.LoginView2.FindControl("isShownEmailCheck3") as CheckBox;

            var userIDs = from a in dcdc.place_agent_tables
                          where a.suburbid == placeID
                          select a;
            Guid theUserId;
            List<agent_info_table> allUser = new List<agent_info_table>();
            foreach (place_agent_table pat in userIDs)
            {
                theUserId = new Guid(pat.UserID.ToString());
                var users = from a in dcdc.agent_info_tables
                            where a.UserID == theUserId && a.UserID != currtneUser
                            select a;

                List<agent_info_table> us = users.ToList<agent_info_table>();
                if (us.Count > 0)
                {
                    allUser.Add(us[0]);
                }

            }

            //fill second user
            if (allUser.Count > 0)
            {
                agent_info_table ait2 = allUser[0];
                Image2.ImageUrl = ait2.Photo;
                nameLabel2.Text = ait2.address;
                phoneLabel2.Text = ait2.Phone;
                mobileLabel2.Text = ait2.Mobile;
                facsimileLabel2.Text = ait2.Fax;
                var theU = from u in dcdc.aspnet_Memberships
                           where u.UserId == ait2.UserID
                           select u.Email;
                String email2 = theU.ToList<String>()[0];
                sendEmailButton2.NavigateUrl = "mailto:" + email2;
                agentId2.Value = ait2.UserID.ToString();

                addressTxt2.Text = ait2.address;
                nameTxt2.Text = ait2.FirstName;
                mobileTxt2.Text = ait2.Mobile;
                phoneTxt2.Text = ait2.Phone;
                faxTxt2.Text = ait2.Fax;
                textTxt2.Text = ait2.Text;
                linkTxt2.Text = ait2.link;
                isShownEmailCheck2.Checked = ait2.IsShowEmail.Value;
            }
            else {
                ImageButton UpdateButton2 = this.LoginView2.FindControl("UpdateButton2") as ImageButton;
                UpdateButton2.Visible = false;
                ImageButton UpdateButton3 = this.LoginView2.FindControl("UpdateButton3") as ImageButton;
                UpdateButton3.Visible = false;
            }
            if (allUser.Count > 1)
            {
                agent_info_table ait3 = allUser[1];
                Image3.ImageUrl = ait3.Photo;
                nameLabel3.Text = ait3.FirstName;
                phoneLabel3.Text = ait3.Phone;
                mobileLabel3.Text = ait3.Mobile;
                facsimileLabel3.Text = ait3.Fax;
                var theU = from u in dcdc.aspnet_Memberships
                           where u.UserId == ait3.UserID
                           select u.Email;
                String email3 = theU.ToList<String>()[0];
                sendEmailButton3.NavigateUrl = "mailto:" + email3;
                agentId3.Value = ait3.UserID.ToString();

                addressTxt3.Text = ait3.address;
                nameTxt3.Text = ait3.address;
                mobileTxt3.Text = ait3.Mobile;
                phoneTxt3.Text = ait3.Phone;
                faxTxt3.Text = ait3.Fax;
                textTxt3.Text = ait3.Text;
                linkTxt3.Text = ait3.link;
                isShownEmailCheck3.Checked = ait3.IsShowEmail.Value;
            }
            else {
                ImageButton UpdateButton3 = this.LoginView2.FindControl("UpdateButton3") as ImageButton;
                UpdateButton3.Visible = false;
            }


        }

        private String uploadFile(FileUpload fu)
        {
            String imageUrl = "";
            if (fu.HasFile)
            {
                try
                {
                    if (System.IO.Path.GetExtension(fu.FileName) == ".jpg" || System.IO.Path.GetExtension(fu.FileName) == ".png")
                    {
                        Label errorLabel = this.LoginView2.FindControl("UploadFileError") as Label;
                        errorLabel.Text = "";
                        fu.SaveAs(Server.MapPath("../images/agentPhotos") + "\\" + fu.FileName);
                        imageUrl = "../images/agentPhotos/" + fu.FileName;
                    }
                    else
                    {
                        Label errorLabel = this.LoginView2.FindControl("UploadFileError") as Label;
                        errorLabel.Text = "Must upload Image file";
                        throw new Exception();
                    }
                }
                catch (Exception ex)
                {

                }
            }

            return imageUrl;
        }
    }
}